<?php
declare (strict_types = 1);

namespace app\model;

use think\facade\Cache;
use think\Model;

/**
 * @mixin think\Model
 */
class Account extends Model
{
    protected $name = "app_account";

    protected $pk = "app_id";
    protected $schema = [
        'app_id' => 'string',
        'secret' => 'string',
        'limit_ip' => 'string',
        'off_time' => 'int'
    ];

    protected function setLimitIpAttr($value){
        return implode(',',$value);
    }

    protected function getLimitIpAttr($value){
        return explode(',',$value);
    }

    public function getInfo($app_id)
    {

        $key =$this->name . ":{$app_id}";

        $appInfo=Cache::get($key);
        if (!$appInfo){
            $appInfo=$this->find($app_id);

            if ($appInfo){
                $appInfo=$appInfo->getData();

                if (!env('app_debug')){
                    Cache::set($key,$appInfo);
                }
            }
        }

        return $appInfo;
    }

    public function setIp($app_id,$ip){
        $appInfo=self::find($app_id);
        $appInfo->ip=$ip;
        return $appInfo->save();
    }

    public function setSecret($app_id,$secret){
        $appInfo=self::find($app_id);
        $appInfo->secret=$secret;
        return $appInfo->save();
    }
}
